<template>
  <div class="tab-title-page goods_list">
    <el-divider class="middle_divider" />
    <div class="child_page">
      <div class="common_form_search">
        <el-form ref="searchForm" :inline="true" :model="searchForm">
          <el-form-item label="商品ID">
            <el-input v-model="searchForm.goods_id" placeholder="请输入商品ID" clearable />
          </el-form-item>
          <el-form-item label="商品名称">
            <el-input v-model="searchForm.goods_name" placeholder="请输入商品名称" clearable />
          </el-form-item>
          <el-form-item label="创建时间">
            <my-date-picker ref="myDatePicker" />
          </el-form-item>
          <el-form-item label="商品品类">
            <el-select v-model="searchForm.cate_id" placeholder="请选择商品品类" clearable>
              <el-option v-for="item in categoryList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" @click="search()">查询</el-button>
            <el-button size="small" @click="reset()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-divider />
      <div style="padding:0 30px">
        <div class="flex align-center table-top-btns">
          <h5 class="flex align-center">
            <svg-icon style="margin-right:15px" icon-class="list" :size="15" />数据列表
          </h5>
					<el-button type="success" size="mini" @click="handleBlind">添加盲盒</el-button>
        </div>
        <el-table v-show="tableData.length > -1" :data="tableData" border>
				<el-table-column align="center" prop="id" label="盲盒id" show-overflow-tooltip="true"  />
				<el-table-column align="center" label="盲盒名称" prop="title"/>
          <el-table-column align="center" label="盲盒图片">
            <template slot-scope="scope">
              <div class="goods_info">
                <el-image style="width: 30px; height: 30px" :src="scope.row.img ? $globalObj.imgBaseUrl + scope.row.img : ''">
                  <div slot="error" class="image-slot">
                    <i class="el-icon-picture-outline" />
                  </div>
                </el-image>
              </div>
            </template>
          </el-table-column>
          <!-- <el-table-column align="center" prop="price" label="售价" show-overflow-tooltip="true"  /> -->
          <el-table-column align="center" label="库存" prop="sell_num" />
          <!-- <el-table-column align="center" prop="cate_name" label="品类" /> -->
          <el-table-column align="center" prop="create_time" label="创建时间" width="180" />
          <el-table-column align="center" label="状态">
						<template slot-scope="scope">
							<span>{{scope.row.status==2?'下架':'上架'}}</span>
						</template>
					</el-table-column>
          <el-table-column fixed="right" align="center" label="操作" width="340">
            <template slot-scope="scope">
              <el-dropdown v-if="$hasMethod('#edit')" style="margin-right:10px" @command="command => handleCommand(command, scope.row.id)">
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="1">商品信息</el-dropdown-item>
                  <el-dropdown-item command="2">套餐信息</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <el-button type="warning" size="mini" @click="changeStatus(scope.row.id, scope.row.status)">{{ scope.row.status == 2 ? '下架' : '上架' }}
              </el-button>
              <!-- <el-button type="success" size="mini" @click="openSelect">设置商品</el-button> -->
							<el-button type="danger" size="mini" @click="handleDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <custom-page ref="customPage" :total="total" @getList="getList" />
				<goods-dialog ref="goodsDialog" dialog-width="1340px" :page-size="12" :append-to-body="true" :show-type-select="true" :show-checked-tool="true" @setSelect="setSelect" />
				<set-blind ref="setBlind" @getList="getList"></set-blind>
      </div>
    </div>
  </div>
</template>
<script>
import myDatePicker from '@/components/myDatePicker'
import customPage from '@/components/customPage'
import goodsDialog from './components/batchDialog'
import setBlind from './components/setBlind'
import { goodsGrounding, getGoodsAll, getCategory, updateGoods, getBoxList, updateBoxDown, updateBoxUp, deleteBox } from '@/api/audit'
export default {
	components: { myDatePicker,customPage, goodsDialog, setBlind },
  data() {
    return {
      searchForm: {
        goods_id: '',
        goods_name: '',
        shop_name: '',
        cate_id: '',
        rent_type: '',
        create_time: ''
      },
      activeName: '1',
      count: {
        status_put: 0,
        status_off: 0,
        status_lease: 0
      },
      total: 0,
      tableData: [],
      categoryList: [],
      goodsList: {}
    }
  },
  mounted() {
    // this.getSelectList()
    this.getList()
  },
  methods: {
    handleBlind() {
      this.$refs.setBlind.dialogVisible = true
    },
    // 设置活动
    setActivity(id) {
			console.log(id);
      this.$refs.setDialog.goodsId = id
      this.$refs.setDialog.dialogVisible = true
    },
		handleDel(e){
			this.$confirm('您确定要删除盲盒吗？', '下架架', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消'
			}).then(() => {
			  deleteBox(e).then(res=>{
			  	this.getList(true)
			  })
			}).catch(() => { })
			
		},
    // 上架/下架
    changeStatus(id, status) {
      if (status === 1) {
        // 下架
				this.$confirm('您确定要下架盲盒吗？', '下架架', {
				  confirmButtonText: '确定',
				  cancelButtonText: '取消'
				}).then(() => {
				  updateBoxDown(id).then(res => {
				    this.getList(true)
				  })
				}).catch(() => { })
      } else {
        // 上架
        this.$confirm('您确定要上架盲盒吗？', '上架', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          updateBoxUp(id).then(res => {
            this.getList(true)
          })
        }).catch(() => { })
      }
    },
    handleMore(command, goodsId, row) {
      if (command === '1') {
        // 查看
        this.$router.push({ name: 'goodsView', params: { goodsId }})
      } else if (command === '2') {
        // 分享链接
        this.$refs.shareDialog.goodsId = goodsId
        this.$refs.shareDialog.dialogVisible = true
      } else if (command === '3') {
        this.goodsList = row
        // // 商品租赁频道
        this.$refs.leaseDialog.goodsId = goodsId
        this.$refs.leaseDialog.dialogVisible = true
      } else if (command === '4') {
        // console.log(goodsId)
        updateGoods({ goods_id: goodsId }).then(res => {
          // console.log(res)
        })
      }
    },
    handleCommand(command, goodsId) {
      if (command === '1') {
        this.$router.push({ name: 'goodsBaseEdit', params: { editId: goodsId }})
      } else {
        this.$router.push({ name: 'goodsMealInfo', params: { goodsId }})
      }
    },
    // 查询
    search() {
      const pickerVal = this.$refs.myDatePicker.getVal()
      if (pickerVal !== 'error') {
        this.searchForm.create_time = pickerVal
        this.$refs.customPage.page = 1
        this.getList()
      }
    },
    // 重置
    reset() {
      if (this.$refs.myDatePicker) {
        this.$refs.myDatePicker.reset()
      }
      this.searchForm = {
        goods_id: '',
        goods_name: '',
        shop_name: '',
        cate_id: '',
        rent_type: '',
        create_time: ''
      }
      this.getList()
    },
    handleClick(tab) {
      this.activeName = tab.name
      this.$refs.customPage.page = 1
      this.getList()
    },
    // 获取数据
    getList(flag) {
      // flag为true, 则不刷新表格
      const { page, pageSize } = this.$refs.customPage
      if (!flag) {
        this.tableData = []
      }
      getBoxList({
        page,
        pageSize,
				boxCreateTime:'',
				title:'',
				id:''
      }).then(res => {
        this.tableData = res.data || []
        this.total = res.total
        for (const key in this.count) {
          this.count[key] = res.count[key]
        }
      })
    },
    getSelectList() {
      getCategory().then(res => {
        this.categoryList = res || []
      })
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.getList(true)
    })
  }
}
</script>
<style lang="scss" scoped>
.common_form_search {
  padding: 15px 30px 0;
}

.child_page {
  padding-bottom: 15px;
}

.goods_info {
  .info {
    // margin-left: 10px;

    p {
      color: #666666;
    }

    p:first-child {
      color: #999999;
    }
  }
}

.activity {
  line-height: 24px;

  span {
    width: 20px;
    height: 20px;
    margin-left: 10px;
  }
}
</style>
